<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">
<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <!-- viewport视口：网页可以根据设置的宽度自动进行适配，在浏览器的内部虚拟一个容器，容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比，为1:1 -->
<!--    <meta name="viewport" content="width=device-width, initial-scale=1">-->
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>


    <title>在线音乐服务器</title>

    <link rel="shortcut icon" href="#" />

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="js/jquery-3.1.1.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
   <!-- <script src="js/bootstrap.min.js"></script>-->
    <style type="text/css">
        td, th {
            text-align: center;
        }
    </style>

    <script type="text/javascript">
        // 核心代码实现
		$(function(){
            load();
        });



        //musicName可以传参  也可以不传参数
        function load(musicName) {

            $.ajax({

                url:"/music/findmusic",
                data:{"musicName": musicName},
                type:"GET",
                dataType:"json",
                success:function(obj) {
                    console.log(obj);
                    var data = obj.data;

                    var s = '';
                    for(var i = 0; i < data.length; i++){
                        var musicUrl = data[i].url+".mp3";
                        console.log(musicUrl);

                        s += '<tr>';
                        s += '<th> <input id = "'+data[i].id+'" type = "checkbox"> </th>';
                        s += '<td>' +data[i].title+'</td>';
                        s += '<td>' +data[i].singer+'</td>';
                        s += '<td> <button class="btn btn-primary" onclick="playerSong(\''+musicUrl+'\')">播放歌曲</button> '+'</td>';
                        s += '<td> <button class="btn btn-primary" onclick="deleteInfo('+data[i].id+')">删除</button>' + 
                         ' <button class="btn btn-primary" onclick="loveInfo('+data[i].id+')">喜欢</button>'+'</td>';
                        s += '</tr>';
                    }
                    $("#info").html(s);

                }
            });
        }





        function playerSong(obj) {
            console.log(obj)
            var name = obj.substring(obj.lastIndexOf('=')+1);
            //obj:播放的音乐的地址  name:播放的音乐的名称，0：播放的开始时间  false:不自动播放
            SewisePlayer.toPlay(obj,name,0,true);
        }

        function deleteInfo(obj) {
            console.log(obj);
            $.ajax({
                url:"/music/delete",
                type: "POST",
                data:{"id":obj},
                dataType:"json",

                success: function(val) {
                    console.log(val);

                    if(val.data == true) {
                        //删除成功！
                        alert("删除成功！,重新加载当前页面！");
                        window.location.href = "list.html";
                    }else{
                        alert("删除失败！");
                    }
                }
            });
        }

        $(function(){
            $("#submit1").click(function(){

                var name = $("#exampleInputName2").val();
                load(name);
            });
       
        $.when(load).done(function(){

            $("#delete").click(function(){

                var id = new Array();
                var i = 0;
                $("input:checkbox").each(function(){

                    if($(this).is(":checked")){
                        id[i] = $(this).attr("id");
                        i++;
                    }
                });

                console.log(id);

                $.ajax({


                    url:"/music/deleteSel",
                    data:{"id":id},
                    dataType:"json",
                    type:"POST",

                    success:function(obj){
                        if(obj.data == true){
                            alert("删除成功!");
                            window.location.href = "list.html";
                        }else{
                            alert("删除失败!");
                        }
                    }
                });
            });
        });
    });



    function loveInfo(obj){
        $.ajax({
            url:"/lovemusic/likeMusic",
            type:"POST",
            data:{"id":obj},
            dataType:"json",

            success:function(val){

                if(val.data == true){
                    alert("收藏成功!");
                    window.location.href = " list.html";
                }else{
                    alert("收藏失败!");
                }
            }
        });
    }

     


    </script>
</head>

<body id="body">
<div class="container">
    <h3  style="text-align: center">乐动网</h3>
    <div style="float: left;">
        <form class="form-inline">
            <div class="form-group">
                <label for="exampleInputName2">歌曲名</label>
                <input name="name" type="text" class="form-control" id="exampleInputName2" >
            </div>
            <button id="submit1" type="button" class="btn btn-primary">查询</button>
        </form>
    </div>
    <div style="float: right;margin-bottom: 15px">
        <a class="btn btn-primary" href="loveMusic.html">喜欢列表</a>
        <a class="btn btn-primary" href="upload.html">添加歌曲</a>
        <a id="delete" class="btn btn-primary">删除选中</a>
    </div>
    <table border="1" class="table table-bordered table-hover">
        <tr class="success">
            <th>选择</th>
            <th>歌名</th>
            <th>歌手</th>
            <th>歌曲</th>
            <th>操作</th>
        </tr>

        

        <tbody id="info" valign="middle">
            <tr>
                <th> <input id="1" type="checkbox"> </th>
                <td>成都</td>
                <td>赵雷</td>
                <td><button class="btn btn-primary" onclick="playerSong('+musicUrl+')">播放歌曲</button></td>
                <td><button class="btn btn-primary" onclick="deleteInfo('+musicUrl+')">删除</button>
                     <button class="btn btn-primary" onclick="loveInfo('+musicUrl+')">喜欢</button>  
                </td>
            </tr>
    
            <tr>
                <th> <input id="2" type="checkbox"> </th>
                <td>成都</td>
                <td>赵雷</td>
                <td><button class="btn btn-primary" onclick="playerSong('+musicUrl+')">播放歌曲</button></td>
                <td><button class="btn btn-primary" onclick="deleteInfo('+musicUrl+')">删除</button>
                     <button class="btn btn-primary" onclick="loveInfo('+musicUrl+')">喜欢</button>  
                </td>
            </tr>
        </tbody>
    </table>
</div>
</body>
<div style="width: 180px; height: 140px; position:absolute; bottom:10px; right:10px">
    <script type="text/javascript" src="player/sewise.player.min.js"></script>
    <script type="text/javascript">
			SewisePlayer.setup({
				server: "vod",
				type: "mp3",
                //这里是默认的一个网址
				videourl:"http://jackzhang1204.github.io/materials/where_did_time_go.mp3",
		        skin: "vodWhite",
                //这里需要设置false
                autostart:"false",
			});
	</script>
</div>
</html>
